package com.lc.zpyh.ui.fragment.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppFragment;
import com.lc.zpyh.http.request.SelectMessagesApi;
import com.lc.zpyh.http.response.SelectMessageBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.ui.activity.home.FoodNewsActivity;
import com.lc.zpyh.ui.activity.home.NoticeDeatailActivity;
import com.lc.zpyh.util.SPUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class TransactionNoticeFragment extends AppFragment<FoodNewsActivity> {
    private static final String ARG_PARAM1 = "param1";

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    // TODO: Rename and change types of parameters
    private String system_id;

    private BaseQuickAdapter<SelectMessageBean.MessagesBean, BaseViewHolder> aadapter;
    List<String> dataBeanList=new ArrayList<>();
    public static TransactionNoticeFragment newInstance(String system_id) {
        TransactionNoticeFragment fragment = new TransactionNoticeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, system_id);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            system_id = getArguments().getString(ARG_PARAM1);

        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


    }

    @Override
    public void onStart() {
        super.onStart();
        selectMessages(system_id);
    }

    private void selectMessages(String system_id) {
        EasyHttp.post(this)
                .api(new SelectMessagesApi().setUserid(SPUtil.get(getContext(), IntentKey.USERID, "")).setType(system_id).setPageNum("1").setPageSize("9999")).request(new HttpCallback<SelectMessageBean>(this) {

            @Override
            public void onSucceed(SelectMessageBean data) {
                if (data.getCode()==0) {

                        aadapter = new BaseQuickAdapter<SelectMessageBean.MessagesBean, BaseViewHolder>
                                (R.layout.item_transactionnotice,data.getMessages()) {

                            @Override
                            protected void convert(BaseViewHolder baseViewHolder,SelectMessageBean.MessagesBean dataBean) {
if (dataBean.getMessageState()==1){
    baseViewHolder.setVisible(R.id.v1,false);
}else {
    baseViewHolder.setVisible(R.id.v1,true);
}
                                baseViewHolder.setText(R.id.tv_type,dataBean.getMessageTheme());
                                baseViewHolder.setText(R.id.tv_neirong,dataBean.getNoticeContent());
                                baseViewHolder.setText(R.id.tv_date,dataBean.getSendTime());
                            }

                        };

                        aadapter.setOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                                startActivity(new Intent(getContext(), NoticeDeatailActivity.class).putExtra("id",aadapter.getData().get(position).getMessageId()+""));

                            }
                        });
                        recyclerView.setAdapter(aadapter);
                        View emptyview = View.inflate(getContext(), R.layout.empty_emptylayout, null);

                        aadapter.setEmptyView(emptyview);

                }
            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_transactionnotice;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }
}
